import {getDriverTask,handleConfirm,handleCancel,handleRefuse} from "@/api/task"
import Dialog from '@vant/weapp/dialog/dialog';
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    role: 2, //角色名：1司机、2承运商
    afootTask: {}
  },

  onShow() {
    this.getDriverTask()
  },

  //获取司机正在进行中的任务
  async getDriverTask(){
      const { driverId = '' } =  app.globalData.userInfo || {}
      const status = 0
      const data = await getDriverTask(driverId,status)
      this.setData({
        afootTask: data[0] || []
      })
  },

  //接收任务
  handleConfirm(e){
    const { code = '' } = e.currentTarget.dataset || {}
    Dialog.confirm({
      title: '接收',
      message: '确定接收此任务吗？',
  }).then(async ()=>{
    await handleConfirm(code)
    wx.showToast({
      title: '接收成功',
      icon: 'success'
    })
    this.getDriverTask()
  }).catch(() => {
    wx.showToast({
        title: '取消',
        icon: 'error'
      })
});
},

  //拒绝任务
  handleRefuse(e){
    const { code = '' } = e.currentTarget.dataset || {}
    Dialog.confirm({
      title: '拒绝',
      message: '确定拒绝此任务吗？',
  }).then(async ()=>{
    await handleRefuse(code)
    wx.showToast({
      title: '拒绝成功',
      icon: 'success'
    })
    this.getDriverTask()
  }).catch(() => {
    wx.showToast({
        title: '取消',
        icon: 'error'
      })
});
  },

  //作废任务
  handleCancel(e){
    const { code = '' } = e.currentTarget.dataset || {}
    Dialog.confirm({
      title: '作废任务',
      message: '确定作废此任务吗？',
  }).then(async ()=>{
    await handleCancel(code)
    wx.showToast({
      title: '作废成功',
      icon: 'success'
    })
    this.getDriverTask()
  }).catch(() => {
    wx.showToast({
        title: '取消',
        icon: 'error'
      })
  });
 },

  // 磅单信息
  handleUpload() {
    const { afootTask } = this.data;
    wx.navigateTo({
        url: `/pages/order/addOrder/addOrder?planAllocationCode=${afootTask.planAllocationCode}&status=${afootTask.status}`,
    })
},
})